﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wedo.Mvc.Utility; 

namespace Wedo.Mvc.Utility
{
    /// <summary>
    /// Url 权限
    /// </summary>
    public class UrlFilter : FilterAttribute, IActionFilter
    {

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
           
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            CurrentUser user = filterContext.HttpContext.User.GetCurrentUserInfo();
            if (user != null)
            {
                string controller = filterContext.RouteData.GetRequiredString("controller");
                string action = filterContext.RouteData.GetRequiredString("action");
                if (CompetenceConfig.ActionCompetence.HadCompetence(controller, action, user.Roles))
                {
                    return;
                }
            }

            ViewResultBase viewResult = null;

            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                viewResult = new PartialViewResult();
            }
            else
            {
                viewResult = new ViewResult();
            }

            viewResult.ViewName = "Error401";
            filterContext.Result = viewResult;
        }
 
    }
}